/*      */ package com.iris.egrant.orm.hibernate;
/*      */ 
/*      */ import com.iris.egrant.hibernate.IrisAliasToEntityMapResultTransformer;
/*      */ import com.iris.egrant.orm.Page;
/*      */ import com.iris.egrant.orm.PropertyFilter;
/*      */ import com.iris.egrant.utils.ReflectionUtils;
/*      */ import java.io.Serializable;
/*      */ import java.math.BigDecimal;
/*      */ import java.sql.Connection;
/*      */ import java.sql.PreparedStatement;
/*      */ import java.sql.ResultSet;
/*      */ import java.sql.ResultSetMetaData;
/*      */ import java.sql.SQLException;
/*      */ import java.sql.Statement;
/*      */ import java.util.ArrayList;
/*      */ import java.util.Collections;
/*      */ import java.util.HashMap;
/*      */ import java.util.Iterator;
/*      */ import java.util.List;
/*      */ import java.util.Map;
/*      */ import java.util.Set;
/*      */ import org.apache.commons.collections.CollectionUtils;
/*      */ import org.apache.commons.lang.ObjectUtils;
/*      */ import org.apache.commons.lang.StringUtils;
/*      */ import org.apache.commons.lang.math.NumberUtils;
/*      */ import org.hibernate.Criteria;
/*      */ import org.hibernate.Query;
/*      */ import org.hibernate.SQLQuery;
/*      */ import org.hibernate.Session;
/*      */ import org.hibernate.SessionFactory;
/*      */ import org.hibernate.criterion.CriteriaSpecification;
/*      */ import org.hibernate.criterion.Criterion;
/*      */ import org.hibernate.criterion.Disjunction;
/*      */ import org.hibernate.criterion.MatchMode;
/*      */ import org.hibernate.criterion.Order;
/*      */ import org.hibernate.criterion.Projection;
/*      */ import org.hibernate.criterion.Projections;
/*      */ import org.hibernate.criterion.Restrictions;
/*      */ import org.hibernate.criterion.SimpleExpression;
/*      */ import org.hibernate.engine.SessionFactoryImplementor;
/*      */ import org.hibernate.hql.QueryTranslator;
/*      */ import org.hibernate.hql.ast.ASTQueryTranslatorFactory;
/*      */ import org.hibernate.impl.CriteriaImpl;
/*      */ import org.hibernate.transform.ResultTransformer;
/*      */ import org.hibernate.transform.Transformers;
/*      */ import org.hibernate.type.BigDecimalType;
/*      */ import org.hibernate.type.CharacterType;
/*      */ import org.hibernate.type.DateType;
/*      */ import org.hibernate.type.DoubleType;
/*      */ import org.hibernate.type.IntegerType;
/*      */ import org.hibernate.type.LongType;
/*      */ import org.hibernate.type.StringType;
/*      */ import org.hibernate.type.Type;
/*      */ import org.springframework.dao.DataAccessException;
/*      */ import org.springframework.dao.InvalidDataAccessApiUsageException;
/*      */ import org.springframework.jdbc.core.BatchPreparedStatementSetter;
/*      */ import org.springframework.jdbc.core.InterruptibleBatchPreparedStatementSetter;
/*      */ import org.springframework.jdbc.core.ParameterDisposer;
/*      */ import org.springframework.jdbc.core.PreparedStatementCallback;
/*      */ import org.springframework.jdbc.core.PreparedStatementCreator;
/*      */ import org.springframework.jdbc.core.SqlProvider;
/*      */ import org.springframework.jdbc.core.StatementCallback;
/*      */ import org.springframework.jdbc.support.JdbcUtils;
/*      */ import org.springframework.util.Assert;
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ public class HibernateDao<T, PK extends Serializable>
/*      */   extends SimpleHibernateDao<T, PK>
/*      */ {
/*      */   public HibernateDao() {}
/*      */   
/*      */   public HibernateDao(SessionFactory sessionFactory, Class<T> entityClass) {
/*   84 */     super(sessionFactory, entityClass);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public Page<T> getAll(Page<T> page) {
/*   93 */     return findPage(page, new Criterion[0]);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public Page<T> findPage(Page<T> page, String hql, Object... values) {
/*  110 */     Assert.notNull(page, "page不能为空");
/*      */     
/*  112 */     Query q = createQuery(hql, values);
/*      */     
/*  114 */     if (page.isAutoCount()) {
/*  115 */       long totalCount = countHqlResult(hql, values);
/*  116 */       page.setTotalCount(Long.valueOf(totalCount));
/*      */     } 
/*      */     
/*  119 */     setPageParameter(q, page);
/*  120 */     List result = q.list();
/*  121 */     page.setResult(result);
/*  122 */     return page;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public Page<T> findPage(Page<T> page, String hql, Map<String, Object> values) {
/*  139 */     Assert.notNull(page, "page不能为空");
/*      */     
/*  141 */     Query q = createQuery(hql, values);
/*      */     
/*  143 */     if (page.isAutoCount()) {
/*  144 */       long totalCount = countHqlResult(hql, values);
/*  145 */       page.setTotalCount(Long.valueOf(totalCount));
/*      */     } 
/*      */     
/*  148 */     setPageParameter(q, page);
/*  149 */     List result = q.list();
/*  150 */     page.setResult(result);
/*  151 */     return page;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public Page<T> findPage(Page<T> page, Criterion... criterions) {
/*  166 */     Assert.notNull(page, "page不能为空");
/*      */     
/*  168 */     Criteria c = createCriteria(criterions);
/*      */     
/*  170 */     if (page.isAutoCount()) {
/*  171 */       int totalCount = countCriteriaResult(c);
/*  172 */       page.setTotalCount(Integer.valueOf(totalCount));
/*      */     } 
/*      */     
/*  175 */     setPageParameter(c, page);
/*  176 */     List result = c.list();
/*  177 */     page.setResult(result);
/*  178 */     return page;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   protected Query setPageParameter(Query q, Page<T> page) {
/*  186 */     q.setFirstResult(page.getFirst().intValue() - 1);
/*  187 */     q.setMaxResults(page.getPageSize().intValue());
/*  188 */     return q;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   protected Criteria setPageParameter(Criteria c, Page<T> page) {
/*  196 */     c.setFirstResult(page.getFirst().intValue() - 1);
/*  197 */     c.setMaxResults(page.getPageSize().intValue());
/*      */     
/*  199 */     if (page.isOrderBySetted()) {
/*  200 */       String[] orderByArray = StringUtils.split(page.getOrderBy(), ',');
/*  201 */       String[] orderArray = StringUtils.split(page.getOrder(), ',');
/*      */       
/*  203 */       Assert.isTrue((orderByArray.length == orderArray.length), "分页多重排序参数中,排序字段与排序方向的个数不相等");
/*      */       
/*  205 */       for (int i = 0; i < orderByArray.length; i++) {
/*  206 */         if ("asc".equals(orderArray[i])) {
/*  207 */           c.addOrder(Order.asc(orderByArray[i]));
/*      */         } else {
/*  209 */           c.addOrder(Order.desc(orderByArray[i]));
/*      */         } 
/*      */       } 
/*      */     } 
/*  213 */     return c;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   protected long countHqlResult(String hql, Object... values) {
/*  222 */     Long count = Long.valueOf(0L);
/*  223 */     String fromHql = hql;
/*      */     
/*  225 */     fromHql = "from " + StringUtils.substringAfter(fromHql, "from");
/*  226 */     fromHql = StringUtils.substringBefore(fromHql, "order by");
/*      */     
/*  228 */     String countHql = "select count(*) " + fromHql;
/*      */     
/*      */     try {
/*  231 */       count = (Long)findUnique(countHql, values);
/*  232 */     } catch (Exception e) {
/*  233 */       throw new RuntimeException("hql can't be auto count, hql is:" + countHql, e);
/*      */     } 
/*  235 */     return count.longValue();
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   protected long countHqlResult(String hql, Map<String, Object> values) {
/*  244 */     Long count = Long.valueOf(0L);
/*  245 */     String fromHql = hql;
/*      */     
/*  247 */     fromHql = "from " + StringUtils.substringAfter(fromHql, "from");
/*  248 */     fromHql = StringUtils.substringBefore(fromHql, "order by");
/*      */     
/*  250 */     String countHql = "select count(*) " + fromHql;
/*      */     
/*      */     try {
/*  253 */       count = (Long)findUnique(countHql, values);
/*  254 */     } catch (Exception e) {
/*  255 */       throw new RuntimeException("hql can't be auto count, hql is:" + countHql, e);
/*      */     } 
/*      */     
/*  258 */     return count.longValue();
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   protected int countCriteriaResult(Criteria c) {
/*  266 */     CriteriaImpl impl = (CriteriaImpl)c;
/*      */ 
/*      */     
/*  269 */     Projection projection = impl.getProjection();
/*  270 */     ResultTransformer transformer = impl.getResultTransformer();
/*      */     
/*  272 */     List<CriteriaImpl.OrderEntry> orderEntries = null;
/*      */     try {
/*  274 */       orderEntries = (List<CriteriaImpl.OrderEntry>)ReflectionUtils.getFieldValue(impl, "orderEntries");
/*  275 */       ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList());
/*  276 */     } catch (Exception e) {
/*  277 */       this.logger.error("不可能抛出的异常:{}", e.getMessage());
/*      */     } 
/*      */ 
/*      */     
/*  281 */     int totalCount = ((Integer)c.setProjection(Projections.rowCount()).uniqueResult()).intValue();
/*      */ 
/*      */     
/*  284 */     c.setProjection(projection);
/*      */     
/*  286 */     if (projection == null) {
/*  287 */       c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
/*      */     }
/*  289 */     if (transformer != null) {
/*  290 */       c.setResultTransformer(transformer);
/*      */     }
/*      */     try {
/*  293 */       ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries);
/*  294 */     } catch (Exception e) {
/*  295 */       this.logger.error("不可能抛出的异常:{}", e.getMessage());
/*      */     } 
/*      */     
/*  298 */     return totalCount;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public List<T> findBy(String propertyName, Object value, PropertyFilter.MatchType matchType) {
/*  310 */     Criterion criterion = buildPropertyFilterCriterion(propertyName, value, matchType);
/*  311 */     return find(new Criterion[] { criterion });
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public List<T> find(List<PropertyFilter> filters) {
/*  318 */     Criterion[] criterions = buildPropertyFilterCriterions(filters);
/*  319 */     return find(criterions);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public Page<T> findPage(Page<T> page, List<PropertyFilter> filters) {
/*  326 */     Criterion[] criterions = buildPropertyFilterCriterions(filters);
/*  327 */     return findPage(page, criterions);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   protected Criterion[] buildPropertyFilterCriterions(List<PropertyFilter> filters) {
/*  334 */     List<Criterion> criterionList = new ArrayList<Criterion>();
/*  335 */     for (PropertyFilter filter : filters) {
/*  336 */       if (!filter.isMultiProperty()) {
/*  337 */         Criterion criterion = buildPropertyFilterCriterion(filter.getPropertyName(), filter.getPropertyValue(), filter.getMatchType());
/*      */         
/*  339 */         criterionList.add(criterion); continue;
/*      */       } 
/*  341 */       Disjunction disjunction = Restrictions.disjunction();
/*  342 */       for (String param : filter.getPropertyNames()) {
/*  343 */         Criterion criterion = buildPropertyFilterCriterion(param, filter.getPropertyValue(), filter.getMatchType());
/*      */         
/*  345 */         disjunction.add(criterion);
/*      */       } 
/*  347 */       criterionList.add(disjunction);
/*      */     } 
/*      */     
/*  350 */     return criterionList.<Criterion>toArray(new Criterion[criterionList.size()]);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   protected Criterion buildPropertyFilterCriterion(String propertyName, Object propertyValue, PropertyFilter.MatchType matchType) {
/*      */     SimpleExpression simpleExpression = null;
/*  358 */     Assert.hasText(propertyName, "propertyName不能为空");
/*  359 */     Criterion criterion = null;
/*      */ 
/*      */     
/*      */     try {
/*  363 */       if (PropertyFilter.MatchType.EQ.equals(matchType)) {
/*  364 */         simpleExpression = Restrictions.eq(propertyName, propertyValue);
/*  365 */       } else if (PropertyFilter.MatchType.LIKE.equals(matchType)) {
/*  366 */         simpleExpression = Restrictions.like(propertyName, (String)propertyValue, MatchMode.ANYWHERE);
/*  367 */       } else if (PropertyFilter.MatchType.LE.equals(matchType)) {
/*  368 */         simpleExpression = Restrictions.le(propertyName, propertyValue);
/*  369 */       } else if (PropertyFilter.MatchType.LT.equals(matchType)) {
/*  370 */         simpleExpression = Restrictions.lt(propertyName, propertyValue);
/*  371 */       } else if (PropertyFilter.MatchType.GE.equals(matchType)) {
/*  372 */         simpleExpression = Restrictions.ge(propertyName, propertyValue);
/*  373 */       } else if (PropertyFilter.MatchType.GT.equals(matchType)) {
/*  374 */         simpleExpression = Restrictions.gt(propertyName, propertyValue);
/*      */       } 
/*  376 */     } catch (Exception e) {
/*  377 */       throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
/*      */     } 
/*  379 */     return (Criterion)simpleExpression;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public boolean isPropertyUnique(String propertyName, Object newValue, Object oldValue) {
/*  388 */     if (newValue == null || newValue.equals(oldValue)) {
/*  389 */       return true;
/*      */     }
/*  391 */     Object object = findUniqueBy(propertyName, newValue);
/*  392 */     return (object == null);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public List queryForList(String sql, Object[] objects) {
/*  404 */     Session session = getSession();
/*      */     
/*  406 */     SQLQuery sqlQuery = (SQLQuery)session.createSQLQuery(sql).setResultTransformer((ResultTransformer)Transformers.ALIAS_TO_ENTITY_MAP);
/*      */ 
/*      */     
/*  409 */     if (objects != null) {
/*  410 */       sqlQuery.setParameters(objects, findTypes(objects));
/*      */     }
/*      */     
/*  413 */     return sqlQuery.list();
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public List queryForListByMap(String sql, Map<String, Object> objects) {
/*  427 */     Session session = getSession();
/*      */     
/*  429 */     SQLQuery sqlQuery = (SQLQuery)session.createSQLQuery(sql).setResultTransformer((ResultTransformer)Transformers.ALIAS_TO_ENTITY_MAP);
/*      */ 
/*      */     
/*  432 */     if (objects != null && !objects.isEmpty()) {
/*  433 */       sqlQuery.setProperties(objects);
/*      */     }
/*      */     
/*  436 */     return sqlQuery.list();
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public List queryForList(String sql) {
/*  447 */     return queryForList(sql, (Object[])null);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public int queryForInt(String sql, Object[] objects) {
/*  459 */     Session session = getSession();
/*      */     
/*  461 */     SQLQuery sqlQuery = session.createSQLQuery(sql);
/*      */     
/*  463 */     if (objects != null) {
/*  464 */       sqlQuery.setParameters(objects, findTypes(objects));
/*      */     }
/*  466 */     Object obj = sqlQuery.uniqueResult();
/*  467 */     String rsValue = "";
/*  468 */     if (obj instanceof Map) {
/*  469 */       Map result = (Map)obj;
/*  470 */       for (Iterator iterator = result.keySet().iterator(); iterator.hasNext(); ) {
/*  471 */         Object key = iterator.next();
/*  472 */         rsValue = ObjectUtils.toString(result.get(key));
/*      */       } 
/*      */     } else {
/*  475 */       rsValue = ObjectUtils.toString(obj);
/*      */     } 
/*  477 */     return NumberUtils.toInt(rsValue);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public int queryForInt(String sql) {
/*  488 */     return queryForInt(sql, (Object[])null);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public long queryForLong(String sql, Object[] objects) {
/*  500 */     Session session = getSession();
/*      */     
/*  502 */     SQLQuery sqlQuery = session.createSQLQuery(sql);
/*      */     
/*  504 */     if (objects != null) {
/*  505 */       sqlQuery.setParameters(objects, findTypes(objects));
/*      */     }
/*  507 */     Object obj = sqlQuery.uniqueResult();
/*  508 */     String rsValue = "";
/*  509 */     if (obj instanceof Map) {
/*  510 */       Map result = (Map)obj;
/*  511 */       for (Iterator iterator = result.keySet().iterator(); iterator.hasNext(); ) {
/*  512 */         Object key = iterator.next();
/*  513 */         rsValue = ObjectUtils.toString(result.get(key));
/*      */       } 
/*      */     } else {
/*  516 */       rsValue = ObjectUtils.toString(obj);
/*      */     } 
/*      */     
/*  519 */     return NumberUtils.toLong(rsValue);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public long queryForLong(String sql) {
/*  531 */     return queryForLong(sql, (Object[])null);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public int update(String sql, Object[] objects) {
/*  542 */     Session session = getSession();
/*      */     
/*  544 */     SQLQuery sqlQuery = session.createSQLQuery(sql);
/*  545 */     if (objects != null) {
/*  546 */       sqlQuery.setParameters(objects, findTypes(objects));
/*      */     }
/*      */     
/*  549 */     int i = sqlQuery.executeUpdate();
/*      */     
/*  551 */     return i;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public int update(String sql) {
/*  562 */     return update(sql, (Object[])null);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public int[] batchUpdate(String sql, final BatchPreparedStatementSetter pss) throws DataAccessException, SQLException {
/*  577 */     if (this.logger.isDebugEnabled()) {
/*  578 */       this.logger.debug("Executing SQL batch update [" + sql + "]");
/*      */     }
/*      */     
/*  581 */     return (int[])execute(sql, new PreparedStatementCallback()
/*      */         {
/*      */           public Object doInPreparedStatement(PreparedStatement ps) throws SQLException
/*      */           {
/*      */             try {
/*  586 */               int batchSize = pss.getBatchSize();
/*  587 */               InterruptibleBatchPreparedStatementSetter ipss = (pss instanceof InterruptibleBatchPreparedStatementSetter) ? (InterruptibleBatchPreparedStatementSetter)pss : null;
/*      */               
/*  589 */               if (JdbcUtils.supportsBatchUpdates(ps.getConnection())) {
/*  590 */                 for (int k = 0; k < batchSize; k++) {
/*  591 */                   pss.setValues(ps, k);
/*  592 */                   if (ipss != null && ipss.isBatchExhausted(k)) {
/*      */                     break;
/*      */                   }
/*  595 */                   ps.addBatch();
/*      */                 } 
/*  597 */                 return ps.executeBatch();
/*      */               } 
/*  599 */               List<Integer> rowsAffected = new ArrayList();
/*  600 */               for (int i = 0; i < batchSize; i++) {
/*  601 */                 pss.setValues(ps, i);
/*  602 */                 if (ipss != null && ipss.isBatchExhausted(i)) {
/*      */                   break;
/*      */                 }
/*  605 */                 rowsAffected.add(new Integer(ps.executeUpdate()));
/*      */               } 
/*  607 */               int[] rowsAffectedArray = new int[rowsAffected.size()];
/*  608 */               for (int j = 0; j < rowsAffectedArray.length; j++) {
/*  609 */                 rowsAffectedArray[j] = ((Integer)rowsAffected.get(j)).intValue();
/*      */               }
/*  611 */               return rowsAffectedArray;
/*      */             } finally {
/*      */               
/*  614 */               if (pss instanceof ParameterDisposer) {
/*  615 */                 ((ParameterDisposer)pss).cleanupParameters();
/*      */               }
/*      */             } 
/*      */           }
/*      */         });
/*      */   }
/*      */ 
/*      */   
/*      */   private Object execute(String sql, PreparedStatementCallback action) throws DataAccessException, SQLException {
/*  624 */     return execute(new SimplePreparedStatementCreator(sql), action);
/*      */   }
/*      */ 
/*      */   
/*      */   private static class SimplePreparedStatementCreator
/*      */     implements PreparedStatementCreator, SqlProvider
/*      */   {
/*      */     private final String sql;
/*      */ 
/*      */     
/*      */     public SimplePreparedStatementCreator(String sql) {
/*  635 */       Assert.notNull(sql, "SQL must not be null");
/*  636 */       this.sql = sql;
/*      */     }
/*      */ 
/*      */     
/*      */     public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
/*  641 */       return con.prepareStatement(this.sql);
/*      */     }
/*      */ 
/*      */     
/*      */     public String getSql() {
/*  646 */       return this.sql;
/*      */     }
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   private Object execute(PreparedStatementCreator psc, PreparedStatementCallback action) throws DataAccessException, SQLException {
/*  658 */     Assert.notNull(psc, "PreparedStatementCreator must not be null");
/*  659 */     Assert.notNull(action, "Callback object must not be null");
/*  660 */     Session session = getSession();
/*  661 */     Connection con = session.connection();
/*  662 */     PreparedStatement ps = null;
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */     
/*      */     try {
/*  669 */       ps = psc.createPreparedStatement(con);
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */       
/*  675 */       Object result = action.doInPreparedStatement(ps);
/*      */       
/*  677 */       return result;
/*  678 */     } catch (SQLException ex) {
/*      */ 
/*      */       
/*  681 */       if (psc instanceof ParameterDisposer) {
/*  682 */         ((ParameterDisposer)psc).cleanupParameters();
/*      */       }
/*      */ 
/*      */       
/*  686 */       psc = null;
/*      */       
/*  688 */       ps.close();
/*  689 */       ps = null;
/*      */       
/*  691 */       con.close();
/*  692 */       con = null;
/*  693 */       throw new SQLException(ex.getMessage() + "批处理失败");
/*      */     } finally {
/*  695 */       if (psc instanceof ParameterDisposer) {
/*  696 */         ((ParameterDisposer)psc).cleanupParameters();
/*      */       }
/*  698 */       if (ps != null) {
/*  699 */         ps.close();
/*      */       }
/*  701 */       if (con != null) {
/*  702 */         con.close();
/*      */       }
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public int[] batchUpdate(final String[] sql) throws SQLException {
/*  717 */     Assert.notEmpty((Object[])sql, "SQL array must not be empty");
/*  718 */     if (this.logger.isDebugEnabled()) {
/*  719 */       this.logger.debug("Executing SQL batch update of " + sql.length + " statements");
/*      */     }
/*      */     class BatchUpdateStatementCallback
/*      */       implements StatementCallback, SqlProvider
/*      */     {
/*      */       private String currSql;
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */       
/*      */       public Object doInStatement(Statement stmt) throws SQLException, DataAccessException {
/*  731 */         int[] rowsAffected = new int[sql.length];
/*  732 */         if (JdbcUtils.supportsBatchUpdates(stmt.getConnection())) {
/*  733 */           for (String element : sql) {
/*  734 */             this.currSql = element;
/*  735 */             stmt.addBatch(element);
/*      */           } 
/*  737 */           rowsAffected = stmt.executeBatch();
/*      */         } else {
/*  739 */           for (int i = 0; i < sql.length; i++) {
/*  740 */             this.currSql = sql[i];
/*  741 */             if (!stmt.execute(sql[i])) {
/*  742 */               rowsAffected[i] = stmt.getUpdateCount();
/*      */             } else {
/*  744 */               throw new InvalidDataAccessApiUsageException("Invalid batch SQL statement: " + sql[i]);
/*      */             } 
/*      */           } 
/*      */         } 
/*  748 */         return rowsAffected;
/*      */       }
/*      */ 
/*      */       
/*      */       public String getSql() {
/*  753 */         return this.currSql;
/*      */       }
/*      */     };
/*      */     try {
/*  757 */       return (int[])execute(new BatchUpdateStatementCallback());
/*  758 */     } catch (DataAccessException e) {
/*  759 */       throw new SQLException(e.getMessage() + "批处理SQL失败");
/*  760 */     } catch (SQLException e) {
/*  761 */       throw new SQLException(e.getMessage() + "批处理SQL失败");
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   private Object execute(StatementCallback action) throws DataAccessException, SQLException {
/*  771 */     Assert.notNull(action, "Callback object must not be null");
/*  772 */     Session session = getSession();
/*  773 */     Connection con = session.connection();
/*  774 */     Statement stmt = null;
/*      */     try {
/*  776 */       stmt = con.createStatement();
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */       
/*  782 */       Object result = action.doInStatement(stmt);
/*      */       
/*  784 */       return result;
/*  785 */     } catch (SQLException ex) {
/*      */ 
/*      */       
/*  788 */       stmt.close();
/*  789 */       stmt = null;
/*  790 */       con.close();
/*  791 */       con = null;
/*  792 */       throw new SQLException(ex.getMessage() + "批处理SQL失败");
/*      */     } finally {
/*  794 */       if (stmt != null) {
/*  795 */         stmt.close();
/*      */       }
/*  797 */       if (con != null) {
/*  798 */         con.close();
/*      */       }
/*      */     } 
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public String getSortSqlPart(Map sortFields, String tableAlias) {
/*  813 */     Set allKeys = sortFields.keySet();
/*  814 */     Iterator itor = allKeys.iterator();
/*  815 */     String temp = "";
/*  816 */     while (itor.hasNext()) {
/*  817 */       String key = String.valueOf(itor.next());
/*  818 */       String value = String.valueOf(sortFields.get(key));
/*      */       
/*  820 */       if ("default".equalsIgnoreCase(value)) {
/*  821 */         value = "asc";
/*      */       }
/*  823 */       temp = temp + "," + tableAlias + "." + key + " " + value + "  nulls last ";
/*      */     } 
/*  825 */     if (!"".equals(temp)) {
/*  826 */       temp = temp.substring(1) + ",";
/*      */     }
/*  828 */     return temp;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public Page queryTable(String sql, Object[] objects, Page page) {
/*  842 */     Session session = getSession();
/*      */     
/*  844 */     SQLQuery sqlQuery = (SQLQuery)session.createSQLQuery(sql).setResultTransformer((ResultTransformer)Transformers.ALIAS_TO_ENTITY_MAP);
/*      */ 
/*      */     
/*  847 */     if (objects != null) {
/*  848 */       sqlQuery.setParameters(objects, findTypes(objects));
/*      */     }
/*  850 */     Long totalCount = Long.valueOf(queryForLong("select count(1) c from (" + sql + ")a", objects));
/*  851 */     page.setTotalCount(totalCount);
/*      */ 
/*      */     
/*  854 */     sqlQuery.setFirstResult(page.getFirst().intValue() - 1);
/*  855 */     sqlQuery.setMaxResults(page.getPageSize().intValue());
/*  856 */     page.setResult(sqlQuery.list());
/*      */ 
/*      */     
/*  859 */     return page;
/*      */   }
/*      */ 
/*      */ 
/*      */   
/*      */   protected Type[] findTypes(Object[] objects) {
/*  865 */     List<Type> list = new ArrayList<Type>();
/*  866 */     for (Object object : objects) {
/*  867 */       if (object instanceof Integer) {
/*  868 */         list.add(new IntegerType());
/*  869 */       } else if (object instanceof Long) {
/*  870 */         list.add(new LongType());
/*  871 */       } else if (object instanceof BigDecimal) {
/*  872 */         list.add(new BigDecimalType());
/*  873 */       } else if (object instanceof Character) {
/*  874 */         list.add(new CharacterType());
/*  875 */       } else if (object instanceof Double) {
/*  876 */         list.add(new DoubleType());
/*  877 */       } else if (object instanceof java.sql.Date) {
/*  878 */         list.add(new DateType());
/*      */       } else {
/*  880 */         list.add(new StringType());
/*      */       } 
/*      */     } 
/*  883 */     return list.<Type>toArray(new Type[0]);
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public String hql2Sql(String hql) {
/*  893 */     if (hql != null && hql.trim().length() > 0) {
/*  894 */       ASTQueryTranslatorFactory aSTQueryTranslatorFactory = new ASTQueryTranslatorFactory();
/*  895 */       SessionFactoryImplementor factory = (SessionFactoryImplementor)this.sessionFactory;
/*  896 */       QueryTranslator translator = aSTQueryTranslatorFactory.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, factory);
/*      */       
/*  898 */       translator.compile(Collections.EMPTY_MAP, false);
/*  899 */       return translator.getSQLString();
/*      */     } 
/*  901 */     return null;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public List<Map<String, Object>> getGridDataInQl(String hql, List<Object> paramList, Integer startIndex, Integer endIndex) {
/*  917 */     Query query = createQuery(hql, paramList).setResultTransformer((ResultTransformer)Transformers.ALIAS_TO_ENTITY_MAP);
/*      */     
/*  919 */     query.setReadOnly(true);
/*  920 */     if (startIndex != null) {
/*  921 */       query.setFirstResult(startIndex.intValue());
/*      */     }
/*  923 */     if (endIndex != null) {
/*  924 */       query.setMaxResults(endIndex.intValue());
/*      */     }
/*  926 */     return query.list();
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public List<Map<String, Object>> getGridDataInQl(String hql, Map<String, Object> paramMap, Integer startIndex, Integer endIndex) {
/*  942 */     Query query = getSession().createQuery(hql).setResultTransformer((ResultTransformer)Transformers.ALIAS_TO_ENTITY_MAP);
/*      */     
/*  944 */     query.setReadOnly(true);
/*  945 */     query.setProperties(paramMap);
/*  946 */     if (startIndex != null) {
/*  947 */       query.setFirstResult(startIndex.intValue());
/*      */     }
/*  949 */     if (endIndex != null) {
/*  950 */       query.setMaxResults(endIndex.intValue());
/*      */     }
/*  952 */     return query.list();
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public List<Map<String, Object>> getGridDataInSql(String sql, List<Object> paramList, Integer startIndex, Integer endIndex) {
/*  969 */     Query query = createSqlQuery(sql, paramList).setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
/*  970 */     query.setReadOnly(true);
/*  971 */     if (startIndex != null) {
/*  972 */       query.setFirstResult(startIndex.intValue());
/*      */     }
/*  974 */     if (endIndex != null) {
/*  975 */       query.setMaxResults(endIndex.intValue());
/*      */     }
/*  977 */     return query.list();
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public List<Map<String, Object>> getGridDataInSql(String sql, Map<String, Object> paramMap, Integer startIndex, Integer endIndex) {
/*  994 */     Query query = getSession().createSQLQuery(sql).setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
/*  995 */     query.setReadOnly(true);
/*  996 */     query.setProperties(paramMap);
/*  997 */     if (startIndex != null) {
/*  998 */       query.setFirstResult(startIndex.intValue());
/*      */     }
/* 1000 */     if (endIndex != null) {
/* 1001 */       query.setMaxResults(endIndex.intValue() - startIndex.intValue());
/*      */     }
/* 1003 */     return query.list();
/*      */   }
/*      */   
/*      */   public Query createQuery(String hql, List<Object> paramList) {
/* 1007 */     return createQuery(hql, paramList.toArray());
/*      */   }
/*      */   
/*      */   public SQLQuery createSqlQuery(String sql, List<Object> paramList) {
/* 1011 */     SQLQuery query = getSession().createSQLQuery(sql);
/* 1012 */     if (CollectionUtils.isEmpty(paramList)) {
/* 1013 */       return query;
/*      */     }
/* 1015 */     Iterator<Object> iterator = paramList.iterator();
/* 1016 */     int i = 0;
/* 1017 */     while (iterator.hasNext()) {
/* 1018 */       query.setParameter(i++, iterator.next());
/*      */     }
/* 1020 */     return query;
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public Integer getTotalRowCount(String ql, List<Object> paramList, String qlModel) {
/* 1031 */     String segment = ql;
/* 1032 */     if (qlModel.equals("hql")) {
/* 1033 */       segment = hql2Sql(ql);
/*      */     }
/* 1035 */     segment = StringUtils.substringAfter(segment.toLowerCase(), "from");
/* 1036 */     String sql = "select count(*) from " + segment;
/* 1037 */     return Integer.valueOf(((BigDecimal)createSqlQuery(sql, paramList).uniqueResult()).intValue());
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public Integer getTotalRowCount(String ql, Map<String, Object> paramMap, String qlModel) {
/*      */     SQLQuery sQLQuery = null;
/* 1050 */     ql = ql.replaceFirst("(\\s[fF][rR][oO][mM]\\s)", " from ");
/* 1051 */     String segment = StringUtils.substringAfter(ql, " from ");
/* 1052 */     ql = "select count(*)  from " + segment + "";
/* 1053 */     Query query = null;
/* 1054 */     if (qlModel.equals("hql")) {
/* 1055 */       query = getSession().createQuery(ql);
/*      */     } else {
/* 1057 */       sQLQuery = getSession().createSQLQuery(ql);
/*      */     } 
/*      */     
/* 1060 */     return Integer.valueOf(((Number)sQLQuery.setProperties(paramMap).uniqueResult()).intValue());
/*      */   }
/*      */ 
/*      */   
/*      */   public List<Map<String, Object>> getDataList(String sql, Map<String, Object> paramMap) {
/* 1065 */     Query query = getSession().createSQLQuery(sql).setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
/* 1066 */     query.setReadOnly(true);
/* 1067 */     query.setProperties(paramMap);
/* 1068 */     return query.list();
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public List queryForList2(String sql, Object[] objects) {
/* 1078 */     Session session = getSession();
/*      */     
/* 1080 */     SQLQuery sqlQuery = (SQLQuery)session.createSQLQuery(sql).setResultTransformer((ResultTransformer)IrisAliasToEntityMapResultTransformer.INSTANCE);
/*      */ 
/*      */     
/* 1083 */     if (objects != null) {
/* 1084 */       sqlQuery.setParameters(objects, findTypes(objects));
/*      */     }
/*      */     
/* 1087 */     return sqlQuery.list();
/*      */   }
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   
/*      */   public List<Map<String, String>> convertList(ResultSet rs) throws SQLException {
/* 1098 */     List<Map<Object, Object>> list = new ArrayList();
/* 1099 */     ResultSetMetaData md = rs.getMetaData();
/* 1100 */     int columnCount = md.getColumnCount();
/* 1101 */     while (rs.next()) {
/* 1102 */       Map<Object, Object> rowData = new HashMap<Object, Object>();
/* 1103 */       for (int i = 1; i <= columnCount; i++) {
/* 1104 */         rowData.put(md.getColumnName(i).toUpperCase(), rs.getObject(i));
/*      */       }
/* 1106 */       list.add(rowData);
/*      */     } 
/* 1108 */     return (List)list;
/*      */   }
/*      */ }


/* Location:              D:\文件kjt\WEB-INF\lib\egrant-core-2.2.20240205.1659.jar!\com\iris\egrant\orm\hibernate\HibernateDao.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       1.1.3
 */